﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.Handlers;
using System.Windows.Forms;
using System.Drawing;
using fideeraSoft.Utils;
using System.Data;
using System.Data.SqlClient;

namespace fideeraSoft.Paneles
{
    public class EnvasadoPanelFin:PanelGeneral
    {
        private System.Windows.Forms.Button btFinalizar;
        private System.Windows.Forms.Button btBuscarEnvasado;
        private System.Windows.Forms.TableLayoutPanel tlpBuscarEnvasado;
        private System.Windows.Forms.TableLayoutPanel tlpBotones;
        private System.Windows.Forms.Button btVolver;
        private System.Windows.Forms.DataGridView dgvEnvasado;
        private System.Windows.Forms.DateTimePicker dtpFechaFin;
        private System.Windows.Forms.Label lbFechaFin;
        private System.Windows.Forms.TableLayoutPanel tlpFinEnv;
        private System.Windows.Forms.TextBox tbBuscarEnvasado;

        private int posicionDataGrid=0;
        private int posicionBuscarProd = 0;
        private int posicionFechaFin = 0;

        private SqlDataAdapter adapter;
        private DataSet ds;
        private DataSet changes;
        private TableLayoutPanel tlpBuscarEntreFechas;
        private DateTimePicker dtpBuscarIni;
        private Button btBuscarEntreFecha;
        private DateTimePicker dtpBuscarFin;
        private SqlCommandBuilder cmdBuilder;
        private GroupBox gbFechaFin;
        private TextBox tbCantPaq;
        private Label lbCantPaq;
        private TextBox tbObservacionFin;
        private Label lbObservacionFin;
        

        private int posicionBuscarEntreFechas = 0;


        public EnvasadoPanelFin(Handler handler)
        {

            this.formatearPanelGeneral(handler);

            this.formatearTableTitulo("FINALIZAR ENVASADO");

            this.InitializeComponent();

             string query = "SELECT e.ID AS [ID Envasado], e.codigo AS [Código de Envasado],e.produccion AS [ID Producción],p.nombre AS [Nombre del Producto], e.fechaInicio AS [Fecha de Inicio] , e.observacion AS Observación FROM Envasado AS e INNER JOIN Produccion AS pr ON e.produccion=pr.ID INNER JOIN Producto AS p ON p.ID=pr.producto WHERE e.fechaFin IS NULL AND e.fechaInicio IS NOT NULL";

            adapter = SQLclass.obtenerAdapter(query);
            if (adapter != null)
            {
                ds = new DataSet();
                adapter.Fill(ds);

                if(ds.Tables[0].Rows.Count<=0)
                    MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN ENVASADA INICIADA CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                else
                    dgvEnvasado.DataSource = ds.Tables[0];

                this.formatearDataGrid();
                this.formatearTablaBuscarProd();
                formatearTablaBuscarEntreFecha();

                this.formatearTablaFin();
                this.formatearTablaBotones();



                this.Controls.Add(dgvEnvasado);
                this.Controls.Add(tlpBuscarEnvasado);

                this.Controls.Add(tlpBuscarEntreFechas);

                this.Controls.Add(gbFechaFin);

                this.Controls.Add(tlpBotones);

            }
            else
            {
                //CLogger.Append("[ProveedorPanelMostrar] Lista Proveedor mostrar Null: ", CLogger.INFO);      //DEBUG
                MessageBox.Show("NO HAY NINGUNA PRODUCCIÓN ENVASADA INICIADA CARGADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.handler.backToPrincipal();
            }
            
        }


        #region FORMATEAR TABLAS

        private void formatearDataGrid()
        {

            posicionDataGrid = this.InitialRealPointVertical + 30;

            dgvEnvasado.AutoGenerateColumns = true;
            dgvEnvasado.Size = new Size(this.Size.Width - 100, 200);
            dgvEnvasado.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvEnvasado.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvEnvasado.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            

            dgvEnvasado.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvEnvasado.Size.Width), posicionDataGrid);
        }

        private void formatearTablaBuscarProd()
        {
            posicionBuscarProd= posicionDataGrid+dgvEnvasado.Size.Height+20;

            tlpBuscarEnvasado.Controls.Add(btBuscarEnvasado);
            tlpBuscarEnvasado.Controls.Add(tbBuscarEnvasado);
                        
            //tlpBuscarProduccion.Size = new Size(400, tbBuscarProduccion.Size.Height + 9);
            tlpBuscarEnvasado.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBuscarEnvasado.Size.Width), posicionBuscarProd);


        }

        private void formatearTablaBuscarEntreFecha()
        {
            posicionBuscarEntreFechas = posicionBuscarProd + tlpBuscarEnvasado.Size.Height + 5;

            tlpBuscarEntreFechas.Controls.Add(btBuscarEntreFecha);
            dtpBuscarIni.Value = System.DateTime.Now;
            dtpBuscarFin.Value = System.DateTime.Now;

            tlpBuscarEntreFechas.Controls.Add(dtpBuscarIni);
            tlpBuscarEntreFechas.Controls.Add(dtpBuscarFin);

            tlpBuscarEntreFechas.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBuscarEntreFechas.Size.Width), posicionBuscarEntreFechas);


        }


        private void formatearTablaFin()
        {
            posicionFechaFin = posicionBuscarEntreFechas + tlpBuscarEntreFechas.Size.Height + 50;

            tlpFinEnv.Controls.Add(lbFechaFin);
            dtpFechaFin.Value = System.DateTime.Now;
            tlpFinEnv.Controls.Add(dtpFechaFin);

            tlpFinEnv.Controls.Add(lbCantPaq);
            tlpFinEnv.Controls.Add(tbCantPaq);

            tlpFinEnv.Controls.Add(lbObservacionFin);
            tlpFinEnv.Controls.Add(tbObservacionFin);
                        
            tlpFinEnv.Location = new Point(MetodosGenerales.calcularCenterWidth(gbFechaFin.Size.Width, this.tlpFinEnv.Size.Width), 30);


            gbFechaFin.Controls.Add(tlpFinEnv);

            gbFechaFin.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.gbFechaFin.Size.Width), posicionFechaFin);

            

        }



        private void formatearTablaBotones()
        {
            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btFinalizar);
            
            //tlpBotones.Size = new Size(400, tbBuscarProvCuil.Size.Height + 9);
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBotones.Size.Width), posicionFechaFin + tlpFinEnv.Size.Height + 100);

        }

        #endregion

        #region INICIALIZAR COMPONENTES
        private void InitializeComponent()
        {
            this.btFinalizar = new System.Windows.Forms.Button();
            this.btBuscarEnvasado = new System.Windows.Forms.Button();
            this.tlpBuscarEnvasado = new System.Windows.Forms.TableLayoutPanel();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            this.btVolver = new System.Windows.Forms.Button();
            this.tbBuscarEnvasado = new System.Windows.Forms.TextBox();
            this.dtpFechaFin = new System.Windows.Forms.DateTimePicker();
            this.lbFechaFin = new System.Windows.Forms.Label();
            this.tlpFinEnv = new System.Windows.Forms.TableLayoutPanel();
            this.tlpBuscarEntreFechas = new System.Windows.Forms.TableLayoutPanel();
            this.dtpBuscarIni = new System.Windows.Forms.DateTimePicker();
            this.btBuscarEntreFecha = new System.Windows.Forms.Button();
            this.dtpBuscarFin = new System.Windows.Forms.DateTimePicker();
            this.gbFechaFin = new System.Windows.Forms.GroupBox();
            this.dgvEnvasado = new System.Windows.Forms.DataGridView();
            this.tbCantPaq = new System.Windows.Forms.TextBox();
            this.lbCantPaq = new System.Windows.Forms.Label();
            this.tbObservacionFin = new System.Windows.Forms.TextBox();
            this.lbObservacionFin = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.dgvEnvasado)).BeginInit();
            this.SuspendLayout();
            // 
            // btFinalizar
            // 
            this.btFinalizar.BackColor = System.Drawing.Color.AliceBlue;
            this.btFinalizar.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btFinalizar.Location = new System.Drawing.Point(0, 0);
            this.btFinalizar.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btFinalizar.Name = "btFinalizar";
            this.btFinalizar.Size = new System.Drawing.Size(75, 40);
            this.btFinalizar.TabIndex = 0;
            this.btFinalizar.Text = "Finalizar";
            this.btFinalizar.UseVisualStyleBackColor = false;
            this.btFinalizar.Click += new System.EventHandler(this.btFinalizar_Click);
            // 
            // btBuscarEnvasado
            // 
            this.btBuscarEnvasado.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarEnvasado.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarEnvasado.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarEnvasado.Location = new System.Drawing.Point(0, 0);
            this.btBuscarEnvasado.Name = "btBuscarEnvasado";
            this.btBuscarEnvasado.Size = new System.Drawing.Size(75, 23);
            this.btBuscarEnvasado.TabIndex = 0;
            this.btBuscarEnvasado.Text = "Buscar Envasado (ID)";
            this.btBuscarEnvasado.UseVisualStyleBackColor = false;
            this.btBuscarEnvasado.Click += new System.EventHandler(this.btBuscarProduccionEnvasadaIni_Click);
            // 
            // tlpBuscarEnvasado
            // 
            this.tlpBuscarEnvasado.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarEnvasado.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarEnvasado.ColumnCount = 2;
            this.tlpBuscarEnvasado.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarEnvasado.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarEnvasado.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarEnvasado.Name = "tlpBuscarEnvasado";
            this.tlpBuscarEnvasado.RowCount = 1;
            this.tlpBuscarEnvasado.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarEnvasado.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarEnvasado.Size = new System.Drawing.Size(460, 40);
            this.tlpBuscarEnvasado.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // tbBuscarEnvasado
            // 
            this.tbBuscarEnvasado.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarEnvasado.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarEnvasado.MaxLength = 10;
            this.tbBuscarEnvasado.Name = "tbBuscarEnvasado";
            this.tbBuscarEnvasado.Size = new System.Drawing.Size(200, 30);
            this.tbBuscarEnvasado.TabIndex = 0;
            // 
            // dtpFechaFin
            // 
            this.dtpFechaFin.CalendarFont = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpFechaFin.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpFechaFin.Location = new System.Drawing.Point(0, 0);
            this.dtpFechaFin.Name = "dtpFechaFin";
            this.dtpFechaFin.Size = new System.Drawing.Size(300, 30);
            this.dtpFechaFin.TabIndex = 0;
            // 
            // lbFechaFin
            // 
            this.lbFechaFin.AutoSize = true;
            this.lbFechaFin.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbFechaFin.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbFechaFin.Location = new System.Drawing.Point(0, 0);
            this.lbFechaFin.Name = "lbFechaFin";
            this.lbFechaFin.Size = new System.Drawing.Size(100, 23);
            this.lbFechaFin.TabIndex = 0;
            this.lbFechaFin.Text = "Fecha Fin Envasado";
            this.lbFechaFin.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tlpFinEnv
            // 
            this.tlpFinEnv.BackColor = System.Drawing.Color.LightCyan;
            this.tlpFinEnv.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpFinEnv.ColumnCount = 2;
            this.tlpFinEnv.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 40F));
            this.tlpFinEnv.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 60F));
            this.tlpFinEnv.Location = new System.Drawing.Point(0, 0);
            this.tlpFinEnv.Name = "tlpFinEnv";
            this.tlpFinEnv.RowCount = 3;
            this.tlpFinEnv.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33F));
            this.tlpFinEnv.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33F));
            this.tlpFinEnv.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33F));
            this.tlpFinEnv.Size = new System.Drawing.Size(550, 120);
            this.tlpFinEnv.TabIndex = 0;
            // 
            // tlpBuscarEntreFechas
            // 
            this.tlpBuscarEntreFechas.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarEntreFechas.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarEntreFechas.ColumnCount = 3;
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 37.5F));
            this.tlpBuscarEntreFechas.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 37.5F));
            this.tlpBuscarEntreFechas.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarEntreFechas.Name = "tlpBuscarEntreFechas";
            this.tlpBuscarEntreFechas.RowCount = 1;
            this.tlpBuscarEntreFechas.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tlpBuscarEntreFechas.Size = new System.Drawing.Size(600, 40);
            this.tlpBuscarEntreFechas.TabIndex = 0;
            // 
            // dtpBuscarIni
            // 
            this.dtpBuscarIni.CalendarFont = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarIni.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarIni.Location = new System.Drawing.Point(0, 0);
            this.dtpBuscarIni.Margin = new System.Windows.Forms.Padding(3, 7, 3, 3);
            this.dtpBuscarIni.Name = "dtpBuscarIni";
            this.dtpBuscarIni.Size = new System.Drawing.Size(300, 23);
            this.dtpBuscarIni.TabIndex = 0;
            // 
            // btBuscarEntreFecha
            // 
            this.btBuscarEntreFecha.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarEntreFecha.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarEntreFecha.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarEntreFecha.Location = new System.Drawing.Point(0, 0);
            this.btBuscarEntreFecha.Name = "btBuscarEntreFecha";
            this.btBuscarEntreFecha.Size = new System.Drawing.Size(75, 23);
            this.btBuscarEntreFecha.TabIndex = 0;
            this.btBuscarEntreFecha.Text = "Buscar Entre Fechas";
            this.btBuscarEntreFecha.UseVisualStyleBackColor = false;
            this.btBuscarEntreFecha.Click += new System.EventHandler(this.btBuscarEntreFecha_Click);
            // 
            // dtpBuscarFin
            // 
            this.dtpBuscarFin.CalendarFont = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarFin.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dtpBuscarFin.Location = new System.Drawing.Point(0, 0);
            this.dtpBuscarFin.Margin = new System.Windows.Forms.Padding(3, 7, 3, 3);
            this.dtpBuscarFin.Name = "dtpBuscarFin";
            this.dtpBuscarFin.Size = new System.Drawing.Size(300, 23);
            this.dtpBuscarFin.TabIndex = 0;
            // 
            // gbFechaFin
            // 
            this.gbFechaFin.Font = new System.Drawing.Font("Bodoni MT", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.gbFechaFin.Location = new System.Drawing.Point(0, 0);
            this.gbFechaFin.Name = "gbFechaFin";
            this.gbFechaFin.Size = new System.Drawing.Size(600, 200);
            this.gbFechaFin.TabIndex = 0;
            this.gbFechaFin.TabStop = false;
            this.gbFechaFin.Text = "Fin de Envasado";
            // 
            // dgvEnvasado
            // 
            this.dgvEnvasado.AllowUserToResizeRows = false;
            this.dgvEnvasado.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.dgvEnvasado.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvEnvasado.Location = new System.Drawing.Point(0, 0);
            this.dgvEnvasado.Name = "dgvEnvasado";
            this.dgvEnvasado.ReadOnly = true;
            this.dgvEnvasado.RowTemplate.DefaultCellStyle.Font = new System.Drawing.Font("Bodoni MT", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dgvEnvasado.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;
            this.dgvEnvasado.Size = new System.Drawing.Size(240, 150);
            this.dgvEnvasado.TabIndex = 0;
            // 
            // tbCantPaq
            // 
            this.tbCantPaq.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbCantPaq.Location = new System.Drawing.Point(0, 0);
            this.tbCantPaq.MaxLength = 8;
            this.tbCantPaq.Name = "tbCantPaq";
            this.tbCantPaq.Size = new System.Drawing.Size(100, 30);
            this.tbCantPaq.TabIndex = 0;
            // 
            // lbCantPaq
            // 
            this.lbCantPaq.AutoSize = true;
            this.lbCantPaq.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbCantPaq.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbCantPaq.Location = new System.Drawing.Point(0, 0);
            this.lbCantPaq.Name = "lbCantPaq";
            this.lbCantPaq.Size = new System.Drawing.Size(100, 23);
            this.lbCantPaq.TabIndex = 0;
            this.lbCantPaq.Text = "Cantidad Paquetes";
            this.lbCantPaq.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tbObservacionFin
            // 
            this.tbObservacionFin.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbObservacionFin.Location = new System.Drawing.Point(0, 0);
            this.tbObservacionFin.MaxLength = 250;
            this.tbObservacionFin.Name = "tbObservacionFin";
            this.tbObservacionFin.Size = new System.Drawing.Size(400, 30);
            this.tbObservacionFin.TabIndex = 0;
            // 
            // lbObservacionFin
            // 
            this.lbObservacionFin.AutoSize = true;
            this.lbObservacionFin.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbObservacionFin.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lbObservacionFin.Location = new System.Drawing.Point(0, 0);
            this.lbObservacionFin.Name = "lbObservacionFin";
            this.lbObservacionFin.Size = new System.Drawing.Size(100, 23);
            this.lbObservacionFin.TabIndex = 0;
            this.lbObservacionFin.Text = "Observación Fin";
            this.lbObservacionFin.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            ((System.ComponentModel.ISupportInitialize)(this.dgvEnvasado)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion


        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }

        private void btFinalizar_Click(object sender, EventArgs e)
        {
            try
            {

                if ((dgvEnvasado != null) && (dgvEnvasado.Rows.Count != 0)&&(ds.Tables[0].Rows.Count!=0)&&(dgvEnvasado.CurrentCell!=null))
                {

                    if((dtpFechaFin == null)||(dtpFechaFin.Value == null)||(string.IsNullOrWhiteSpace(tbCantPaq.Text)))
                    {
                        MessageBox.Show("DATOS DE FINALIZACIÓN NO PUEDEN SER NULOS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    int distance;
                    if (!int.TryParse(tbCantPaq.Text, out distance))
                    {
                        MessageBox.Show("LA CANTIDAD DE PAQUETES DEBE SER UN VALOR NUMÉRICO", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        tbCantPaq.Clear();
                        return;
                    }

                    int indx = dgvEnvasado.CurrentCell.RowIndex;

                    string strDataIdEnvasado = dgvEnvasado[0, indx].Value.ToString();
                    
                    string strIdProduccion = dgvEnvasado[2, indx].Value.ToString();

                    string strProducto = dgvEnvasado[3, indx].Value.ToString();

                    string strDataFechaIni = dgvEnvasado[4, indx].Value.ToString();
                    
                    DateTime dtDateIni = (DateTime) dgvEnvasado[4, indx].Value;

                   // MessageBox.Show("LA FECHA DE INICIO: "+dtDateIni.ToString(), "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                   // MessageBox.Show("LA FECHA DE FIN: " + dtpFechaFin.ToString(), "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    
                    int inRes=DateTime.Compare(dtDateIni,dtpFechaFin.Value);

                    if ( inRes > 0)
                    {
                        MessageBox.Show("LA FECHA DE INICIO NO PUEDE SER MAYOR QUE LA FECHA DE FINALIZACIÓN", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    DialogResult res = MessageBox.Show("Seguro que quiere finalizar la producción envasada iniciada. \n ID Envasado: " + strDataIdEnvasado + "\n Producto: "+strProducto+"\n Fecha Inicio: " + dtDateIni.Date.ToString("d") + "\n Fecha Fin: " + dtpFechaFin.Value.Date.ToString("d") + "\n Cantidad Paquetes: " + tbCantPaq.Text + "\n Observación fin envasado: " + tbObservacionFin.Text, "INFO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (res == DialogResult.Yes)
                    {
                        this.handler.updateFechaFinEnvasadoPorId(strDataIdEnvasado, dtpFechaFin.Value.Date);

                        this.handler.updateCantidadPaquetesEnvasadosPorId(strDataIdEnvasado, tbCantPaq.Text);

                        if (!string.IsNullOrWhiteSpace(tbObservacionFin.Text))
                        {
                            this.handler.updaceObservacionFinEnvasadoEnEnvasadoPorId(strDataIdEnvasado, tbObservacionFin.Text);
                        }

                        this.handler.updateFechaFinEnvasadoEnProduccionPorId(strIdProduccion,dtpFechaFin.Value.Date);

                        this.handler.updateCantidadPaquetesEnvasadosEnProduccionPorId(strIdProduccion, tbCantPaq.Text);

                        

                        MessageBox.Show("Producción envasada finalizada correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        dtpFechaFin.Value=System.DateTime.Now;
                       // this.Refresh();
                        this.handler.backToPrincipal();

                    }
                    else{}
                }
                else
                {
                    MessageBox.Show("No hay ninguna producción envasada comenzada en la lista", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (MiException exc)
            {
                MessageBox.Show("No se pudo finalizar la Producción envasada comenzada", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                CLogger.Append("[btFinalizar_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
            }
        }

        private void btBuscarProduccionEnvasadaIni_Click(object sender, EventArgs e)
        {
            if (dgvEnvasado.CurrentCell==null)
            {
                MessageBox.Show("NO HAY NINGUN VALOR CARGADO EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            dgvEnvasado.ClearSelection();
            if (!string.IsNullOrWhiteSpace(tbBuscarEnvasado.Text))
            {
                string searchValue = tbBuscarEnvasado.Text;


                try
                {
                    foreach (DataGridViewRow row in dgvEnvasado.Rows)
                    {
                        if (row.Cells[0].Value.ToString().Equals(searchValue))
                        {
                            dgvEnvasado.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                            row.Selected = true;

                            //MessageBox.Show("ENCONTRE: "+inRowFinded, "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            dgvEnvasado.CurrentCell = row.Cells[0];
                            tbBuscarEnvasado.Clear();
                            break;

                        }

                    }
                }
                catch (Exception exc)
                {
                    tbBuscarEnvasado.Clear();
                    MessageBox.Show("NO SE ENCONTRO LA PRODUCCIÓN ENVASADA INICIADA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // CLogger.Append("[btBuscarProducto_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
            }
            else
            {
                MessageBox.Show("NO SE INGRESO PRODUCCIÓN ENVASADA INICIADA A BUSCAR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }

        private void dgvProduccionFin_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dgvEnvasado.SelectionMode = DataGridViewSelectionMode.CellSelect;
        }

        private void btBuscarEntreFecha_Click(object sender, EventArgs e)
        {

            if (dgvEnvasado.CurrentCell==null)
            {
                MessageBox.Show("NO HAY NINGUN VALOR CARGADO EN LA TABLA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            dgvEnvasado.ClearSelection();
            if((dtpBuscarIni.Value==null)||(dtpBuscarFin.Value==null))
            {
                MessageBox.Show("VALORES DE FECHA ERRONEOS", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //if (dtpBuscarIni.Value > dtpBuscarFin.Value) 
            if (DateTime.Compare(dtpBuscarIni.Value, dtpBuscarFin.Value) > 0)
            {
                MessageBox.Show("LA FECHA PRIMARIA NO PUEDE SER MAYOR QUE LA FECHA SECUNDARIA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            //MessageBox.Show("FECHA 1: " + dtBuscarIni.Value.ToString("MM/dd/yyyy") + " FECHA 2: " + dtBuscarFin.Value.ToString("MM/dd/yyyy"), "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            //string query = "SELECT ID AS ID_Envasado, codigo AS Codigo,produccion AS ID_Produccion, fechaInicio AS FechaInicio, observacion AS Observacion FROM Envasado WHERE (fechaInicio>='" + dtpBuscarIni.Value + "' AND fechaInicio<='" + dtpBuscarFin.Value + "') AND fechaFin IS NULL";            
            
            //string query = "SELECT e.ID AS idEnvasado, e.codigo AS CódigoEnvasado,e.produccion AS idProducción,p.nombre AS NombreProducto, e.fechaInicio AS FechaInicio , e.observacion AS Observación FROM Envasado AS e INNER JOIN Produccion AS pr ON e.produccion=pr.ID INNER JOIN Producto AS p ON p.ID=pr.producto WHERE e.fechaFin IS NULL AND e.fechaInicio IS NOT NULL";
            string query = "SELECT e.ID AS [ID Envasado], e.codigo AS [Código de Envasado],e.produccion AS [ID Producción],p.nombre AS [Nombre del Producto], e.fechaInicio AS [Fecha de Inicio], e.observacion AS Observación FROM Envasado AS e INNER JOIN Produccion AS pr ON e.produccion=pr.ID INNER JOIN Producto AS p ON p.ID=pr.producto WHERE (e.fechaInicio BETWEEN @FECHAINICIO AND @FECHAFIN) AND e.fechaFin IS NULL"; 
            adapter = SQLclass.obtenerAdapter(query);

            adapter.SelectCommand.Parameters.AddWithValue("@FECHAINICIO", dtpBuscarIni.Value.Date);
            
            adapter.SelectCommand.Parameters.AddWithValue("@FECHAFIN", dtpBuscarFin.Value.Date);

            if (adapter != null)
            {
                ds = new DataSet();
                adapter.Fill(ds);
                                
                if ( ds.Tables[0].Rows.Count <= 0)
                {
                    MessageBox.Show("NO SE ENCONTRARON PRODUCCIONES ENVASADAS INICIADAS ENTRE ESAS FECHAS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                }
                else
                {

                    dgvEnvasado.DataSource = ds.Tables[0];
                }

                
            }
            else
            {
                //CLogger.Append("[ProveedorPanelMostrar] Lista Proveedor mostrar Null: ", CLogger.INFO);      //DEBUG
                MessageBox.Show("ERROR. NO SE ENCONTRARON PRODUCCIONES ENVASADAS INICIADAS ENTRE ESAS FECHAS", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.handler.backToPrincipal();
            }
        }
    }
}
